//
//  OneTypeView.m
//  QuickCure
//
//  Created by 王震 on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "OneTypeView.h"
#import <Masonry.h>
#import "OneDiseaseTypeViewController.h"


@interface OneTypeView ()

@property(nonatomic,copy) NSString *titleString;
@property(nonatomic,strong) UIImage *titleImage;
@property(nonatomic,strong) UIImageView *titleIamgeView;
@property(nonatomic,strong) UILabel *titleLabel;

@end

@implementation OneTypeView

-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        
        //添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

-(void)tap:(UITapGestureRecognizer *)tap{
    
    OneTypeView *oneView = (OneTypeView *)tap.view;
    
    //公益项目网址
    //http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm
    
    
    /**
     *  获取当前显示的viewController
     */
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    UINavigationController *currentVC = currentWindow.rootViewController.childViewControllers.lastObject;
 
    //    //创建疾病类型控制器,点击跳转
    OneDiseaseTypeViewController *OneVC = [[OneDiseaseTypeViewController alloc] init];

    [currentVC pushViewController:OneVC animated:YES];
    OneVC.selectIndex = self.tag;
    OneVC.title = oneView.titleString;
    
    //使用通知,通知当前控制器push下一个控制器
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"deseaseType" object:nil userInfo:@{currentVC.navigationItem.title:oneView.titleString}];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self addSubview:self.titleIamgeView];        
        [self addSubview: self.titleLabel];
    }
    return self;
}


#pragma mark - 快速创建方法
+(instancetype)oneTypeViewWithTitle:(NSString *)titleStr image:(UIImage *)image{
    
    OneTypeView *typeView = [[self alloc] init];
    typeView.titleString = titleStr;
    typeView.titleImage = image;
    
    return typeView;
}

-(void)layoutSubviews{
    
    [self setSubViewsFrame];
    [self setSubViewsData];
}

-(void)setSubViewsFrame{
    
    [self.titleIamgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.mas_bottom).multipliedBy(0.4);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.mas_bottom).multipliedBy(0.8);
    }];
    
}

-(void)setSubViewsData{
    
    [self.titleIamgeView setImage:self.titleImage];
    [self.titleIamgeView setContentMode:UIViewContentModeScaleAspectFit];
    
    [self.titleLabel setText:self.titleString];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [self.titleLabel sizeToFit];
}

#pragma mark - 控件属性的懒加载
/**
 @property(nonatomic,weak) UIImageView *titleIamgeView;
 @property(nonatomic,weak) UILabel *titleLabel;
 */
-(UIImageView *)titleIamgeView{
    if (nil == _titleIamgeView) {
        _titleIamgeView = [[UIImageView alloc] init];
    }
    return _titleIamgeView;
}
-(UILabel *)titleLabel{
    if (nil == _titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}
@end

